package triperest;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Image implements DataObject {
	/** The ID. */
	private long ID = 0;
	
	/** The user id. */
	public String userId;
	
	/** The rank. */
	public Integer rank;
	
	/** The comment. */
	public String comment;
	
	/** The image. */
	public byte imageData[];
	
	/**
	 * Get Id.
	 *
	 * @return the id
	 */
	public long getId() { return ID; }

	/**
	 * Instantiates a new review.
	 */
	public Image(byte[] data){
		rank = 0;
		imageData = data;
	}
	
	/**
	 * Instantiates a new review.
	 *
	 * @param Id the id
	 */
	public Image(long Id)
	{
		ID = Id;
		load();
	}
	
	public void setUser(TriperestUser user)
	{
		userId = user.getId();
	}
	
	public void setComment(String Comment)
	{
		this.comment = Comment;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see triperest.DataObject#delete()
	 */
	@Override
	public boolean delete() {
		try {
			if (DataConnection.connect()) {
				String statement = "delete from image where image_id = ?";
				PreparedStatement stmt = DataConnection.getConnection()
						.prepareStatement(statement);
				stmt.setLong(1, ID);
				int success = 2;
				stmt.executeUpdate();
				return success == 1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see triperest.DataObject#save()
	 */
	@Override
	public boolean save() {
		try {

			if (ID != 0) {
				return update();
			}
			return insert();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Update.
	 * 
	 * @return true, if successful
	 * @throws SQLException
	 *             the SQL exception
	 */
	private boolean update() throws SQLException {
		if (DataConnection.connect()) {
			ByteArrayInputStream bin = new ByteArrayInputStream(imageData);
			String statement = "update image set user_id = ?, rank = ?, comment = ?, image = ? where image_id = ?";
			PreparedStatement stmt = DataConnection.getConnection()
					.prepareStatement(statement);
			stmt.setString(1, userId);
			stmt.setInt(2, rank);
			stmt.setString(3, comment);
			stmt.setBinaryStream(4,bin,imageData.length);
			stmt.setLong(5, ID);
			int success = 2;
			stmt.executeUpdate();
			return success == 1;
		}
		return false;
	}
	
	/**
	 * Insert.
	 * 
	 * @return true, if successful
	 * @throws SQLException
	 *             the sQL exception
	 */
	private boolean insert() throws SQLException {
		if (DataConnection.connect()) {
			ByteArrayInputStream bin = new ByteArrayInputStream(imageData);
			String statement = "INSERT INTO image (user_id, rank, comment, image) VALUES ( ? , ? , ? , ? )";
			PreparedStatement stmt = DataConnection.getConnection()
					.prepareStatement(statement, Statement.RETURN_GENERATED_KEYS);
			stmt.setString(1, userId);
			stmt.setInt(2, rank);
			stmt.setString(3, comment);
			stmt.setBinaryStream(4, bin, imageData.length);
			int success = 2;
			stmt.executeUpdate();
			ResultSet key = stmt.getGeneratedKeys();
			if(key.next())
			{
				ID = key.getLong(1);
			}
			return success == 1;
		}
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see triperest.DataObject#load()
	 */
	@Override
	public boolean load() {
		try {
			if (DataConnection.connect()) {
				String statement = "select user_id, rank, comment, image from image where image_id = ?";
				PreparedStatement stmt = DataConnection.getConnection()
						.prepareStatement(statement);
				stmt.setLong(1, ID);
				ResultSet results = stmt.executeQuery();
				if (results.next()) {
					userId = results.getString("user_id");
					rank = results.getInt("rank");
					comment = results.getString("comment");
					InputStream input = results.getBinaryStream("image");
					input.read(imageData);
					return true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}
